package com.wang;

import com.wang.interceptor.LoginHandlerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.*;

@Component
@Configuration
@EnableWebMvc
public class WebMvcConfig implements WebMvcConfigurer {
    /**
     * 添加静态资源文件，外部可以直接访问地址
     *
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
      //  registry.addResourceHandler("/static/img/*").addResourceLocations("classpath:/static/img/");
    }

    @Configuration
    public class WebConfig extends WebMvcConfigurationSupport {
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry.addResourceHandler("/**").addResourceLocations(
                    "classpath:/static/");
            registry.addResourceHandler("swagger-ui.html").addResourceLocations(
                    "classpath:/META-INF/resources/");
            registry.addResourceHandler("/webjars/**").addResourceLocations(
                    "classpath:/META-INF/resources/webjars/");
            registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
            super.addResourceHandlers(registry);
        }

        /**
         * 这里与LoginHandlerInterceptor中的redis有关，有个先后顺序
         * @return
         */
        @Bean
        LoginHandlerInterceptor loginHandlerInterceptor(){
            return new LoginHandlerInterceptor();
        }

        @Override
        public void addViewControllers(ViewControllerRegistry registry) {
            registry.addViewController("/login").setViewName("admin/login");
            registry.addViewController("*/login").setViewName("admin/login");
        }

/*        @Override
        public void addInterceptors(InterceptorRegistry registry) {
            System.out.println("登陆拦截器启动成功。");
            //加载拦截器
            registry.addInterceptor(loginHandlerInterceptor())
                    //拦截所有路径，"/**"表示该项目下的所有路径
                    .addPathPatterns("/**")
                    //放行的路径
                    .excludePathPatterns("/","/userLogin")
                    .excludePathPatterns("/swagger-resources/**", "/webjars/**", "/v2/**", "/swagger-ui.html/**")
                    //静态资源放行，不拦截静态资源
                    .excludePathPatterns("/static/**");
        }*/
    }

}